﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ContractorTimesheet.DataModel;
using ContractorTimesheet.Infrastructure;
using ContractorTimesheet.Models;
using ContractorTimesheet.Repository;
using SimpleCrypto;

namespace ContractorTimesheet.Controllers
{
    public class ResetPasswordController : Controller
    {
        private UserRepository repository = new UserRepository();

        public ActionResult Index()
        {
            if (CurrentSession.User == null)
                return RedirectToAction("Login", "Home");

            var model = new ResetPasswordModel();

            model.ResetInfo.AddRange(ResetInfo.CreateResetInfo(repository.GetAllUsers()));

            return View(model);
        }

        public ActionResult ResetPassword(ResetPasswordModel model, int index)
        {
            ResetInfo resetInfo = model.ResetInfo[index];

            User user = repository.Get(resetInfo.Id);

            PBKDF2 crypto = new PBKDF2();
            user.Password = crypto.Compute(ConfigurationManager.AppSettings.Get("DefaultPassword"));
            user.PasswordSalt = crypto.Salt;

            repository.UpdateUser(user);

            resetInfo.Message = "Password reset sucessfully.";

            return Json(model);
        }

    }
}